<template>
  <div>
    <div class="container">
      <el-row>
        <el-col :span="12">
          <span style="font-size:18px; font-weight:bold;">文章管理</span>
        </el-col>
        <el-col style="text-align:right;" :span="12">
          <el-button
            @click="
              editVisible = true;
              currentId = null;
            "
            type="primary"
            size="small"
            >添加</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="list"
        style="width: 100%;margin-top:5px;"
        :height="tableHeight"
        border
        stripe
      >
        <el-table-column type="index" width="50"> </el-table-column>
        <el-table-column label="标题" prop="describe"> </el-table-column>
        <el-table-column label="缩略图">
          <template slot-scope="scope">
            <img
              style="height:80px;width:120px;object-fit:cover;"
              :src="scope.row.links[0]"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column label="时间">
          <template slot-scope="scope">
            {{ scope.row.created_at }}
          </template>
        </el-table-column>
        <el-table-column label="是否显示">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_verify"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              @change="dynamicAudit(scope.row)"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button
              @click="
                editVisible = true;
                currentId = scope.row.id;
              "
              type="primary"
              size="small"
              >编辑</el-button
            >
            <el-button @click="deleteArticle(scope.row.id)" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="paging">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="page"
          :page-size="size"
          layout="total, prev, pager, next"
          :total="total"
          :background="true"
        >
        </el-pagination>
      </div>
    </div>
    <edit-article
      :visible="editVisible"
      :id="currentId"
      @close="
        editVisible = false;
        getArticleList(page);
      "
    ></edit-article>
  </div>
</template>

<script>
import { getArticleList, deleteDynamic, dynamicAudit } from "@/api/dynamic-mng";
import editArticle from "./components/edit-article.vue";
export default {
  name: "ArticleMng",
  data() {
    return {
      editVisible: false,
      list: [],
      tableHeight: 0,
      currentId: null,
      total: 0,
      size: 0,
      page: 1
    };
  },
  methods: {
    dynamicAudit(row) {
      dynamicAudit({
        id: row.id,
        is_verify: row.is_verify === 1 ? "agree" : "refuse"
      }).then(res => {
        if (res.code === 200) {
        }
      });
    },
    deleteArticle(id) {
      this.$confirm("确认删除该文章?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteDynamic({ id }).then(res => {
          if (res.code === 200) {
            this.getArticleList(this.page);
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        });
      });
    },
    getArticleList(page) {
      getArticleList({ page }).then(res => {
        if (res.code === 200) {
          this.list = res.data.data;
          this.total = res.data.total;
          this.page = res.data.current_page;
          this.size = res.data.per_page;
        }
      });
    },
    handleCurrentChange(page) {
      this.getArticleList(page);
    },
    changeSize() {
      this.tableHeight = document.body.clientHeight - 180;
      window.onresize = () => {
        this.changeSize();
      };
    }
  },
  created() {
    this.getArticleList(1);
    this.changeSize();
  },
  components: {
    editArticle
  }
};
</script>

<style></style>
